package com.mjh.comm;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Log4jInitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	 static Logger logger = Logger.getLogger(Log4jInitServlet.class);
	 
	 //改为用com.mjh.listener.ContextListener来配置日志输出路径
     public void init(ServletConfig config) throws ServletException {
         String prefix = config.getServletContext().getRealPath("/");
         String file = config.getInitParameter("log4j");
         String filePath = prefix + file;
         Properties props = new Properties();
         try {
             FileInputStream istream = new FileInputStream(filePath);
             props.load(istream);
             istream.close();    
             String logFile = prefix + props.getProperty("log4j.appender.R.File");//设置路径
             //System.out.println(logFile);
             props.setProperty("log4j.appender.R.File",logFile);
             PropertyConfigurator.configure(props);//装入log4j配置信息
         } catch (IOException e) {
             System.out.println("Could not read configuration file [" + filePath + "].");
             System.out.println("Ignoring configuration file [" + filePath + "].");
             return;
         }
     }

}
